let memberData4 = JSON.parse(sessionStorage.getItem('memberData4'));
console.log("doctor_advice memberData4:" + memberData4);
console.log(memberData4);
let yaodianId = 0;
let memberId = 0;
let orderId = 0;
let doctorId = "";
let diagnosisId = "";
let nickName = "";
let headImg = "";
let deptName = "";

if (memberData4 != null) {
    memberId = memberData4.memberId;
    // orderId = memberData4.orderId;
    doctorId = $.cookie("doctorId");
    diagnosisId = memberData4.diagnosisId;
    nickName = $.cookie("truename");
    headImg = memberData4.icon;
    deptName = $.cookie("deptName");
}

//选择单项医技
function yxz(inspectId) {
    $.ajax({
        url: $.cookie("tempUrl") + "PmsInspect/selectById?id=" + inspectId,
        type: "GET",
        success: function (result) {
            if (result.code === 200) {
                $("#select_array").append('<input istype="single" id="' + inspectId + '" class="select-item clear-bg" type="text" value="' + result.data.inspectName + '" disabled="disabled">')
            }
        },
        error: function () {
            layer.msg("请先选择服务医院")
        }
    });
}

//选择医技套餐
function yxzpack(packId) {
    $.ajax({
        url: $.cookie("tempUrl") + "sysdoctorcheckpack/selectById?id=" + packId,
        type: "GET",
        success: function (result) {
            if (result.code === 200) {
                $("#select_array").append('<input istype="more" id="' + packId + '" ids="' + result.data.inspectId + '" class="select-item clear-bg" type="text" value="' + result.data.name + '" disabled="disabled">')
            }
        },
        error: function () {
            layer.msg("请先选择服务医院")
        }
    });
}

//加载热门单项
function rmdx() {
    $("#select_array_list").text("");
    $("#select_array").text("");
    if (!sessionStorage.getItem('pharmacyVO')) {
        layer.msg("请先选择服务医院")
    } else {
        $.ajax({
            url: $.cookie("tempUrl") + "inspect/findPmsInspectListByRm",
            type: "POST",
            success: function (result) {
                if (result.code === 200) {
                    $(result.data).each(function (index, element) {
                        $("#select_array_list").append('<input id="' + element.id + '" class="clear-bg hot-signle" type="button" value="' + element.inspectName + '" onclick="yxz(' + element.id + ')">')
                    });
                }
            },
            error: function () {
                layer.msg("请先选择服务医院")
            }
        });
    }
}

//加载热门套餐
function rmtc() {
    $("#select_array_list").text("");
    $("#select_array").text("");
    if (!sessionStorage.getItem('pharmacyVO')) {
        layer.msg("请先选择服务医院")
    } else {
        let pharmacyVO = JSON.parse(sessionStorage.getItem('pharmacyVO'));
        yaodianId = pharmacyVO.id;
        $.ajax({
            url: $.cookie("tempUrl") + "inspect/findByRm?yyId=" + yaodianId,
            type: "POST",
            success: function (result) {
                if (result.code === 200) {
                    $("#select_array_list").text("")
                    $(result.data).each(function (index, element) {
                        $("#select_array_list").append('<input id="' + element.id + '" class="clear-bg hot-signle" type="button" value="' + element.name + '" onclick="yxzpack(' + element.id + ')">')
                    });
                }
            },
            error: function () {
                layer.msg("请先选择服务医院")
            }
        });
    }
}

$(document).ready(function () {

    $(document).click(function (event) {
        var _con1 = $('#chubuZhenduan'); // 设置目标区域
        var _con2 = $('#txSelect'); // 设置目标区域
        if (!_con1.is(event.target) && !_con2.is(event.target) && _con2.has(event.target).length === 0) {
            $('#txSelect').hide();
        }
        var _con3 = $('.kyIptDiv'); // 设置目标区域
        var _con4 = $('#yaoSearch'); // 设置目标区域
        var _con5 = $('#yaoSearchList'); // 设置目标区域
        if (!_con3.is(event.target) && !_con4.is(event.target) && !_con5.is(event.target) && _con5.has(event.target).length === 0) {
            $('#yaoSearchList').hide();
        }
    });

    $("#selectDrugstore").click(function () {
        parent.parent.layer.open({
            type: 2,
            title: '选择服务医院',
            area: ['900px', '500px'],
            // closeBtn: 0,
            shadeClose: true,
            content: 'page/tool/tool02Add_add.html',
            end: function () {
                let pharmacyVO = JSON.parse(sessionStorage.getItem('pharmacyVO'));
                yaodianId = pharmacyVO.id;
                // layer.msg(yaodianId)
                $("#pharmacyVO").text(pharmacyVO.name)
                // rmdx()
                return false;
            }
        });
    })

    $("#reset").click(function () {
        //清楚原医院记录
        sessionStorage.removeItem('pharmacyVO')
        window.location.reload()
    });

    var selectMode = 1;
    $('input[type=radio][name=mode]').change(function () {
        var label = $("label.mode-radio");
        label.removeClass("mr-select");
        if (this.value == 1) {
            selectMode = 1;
            $(label[0]).addClass("mr-select");
            $("#select_name").text("已选择单项：")
            $("#hot_name").text("※ 热门单项 ※")
        } else if (this.value == 2) {
            selectMode = 2;
            $(label[1]).addClass("mr-select");
            $("#select_name").text("已选择套餐：")
            $("#hot_name").text("※ 热门套餐 ※")
        }
    });

    $('input[type=radio][name=packageMode]').change(function () {
        var label = $("label.mode-radio");
        label.removeClass("mr-select");
        if (this.value == 1) {
            $("#timePackageLabel").addClass("mr-select");
        } else if (this.value == 2) {
            $("#onePckageLabel").addClass("mr-select");
        }
    });
});

function projectSelectChange(res) {
    if (res == 1) {
        $("#selectDrugstore").show();
        $("#prescription").show();
        $("#technician").hide();
        $("#service").hide();
    } else if (res == 2) {
        $("#selectDrugstore").show();
        $("#prescription").hide();
        $("#technician").show();
        $("#service").hide();
    } else {
        $("#selectDrugstore").hide();
        $("#prescription").hide();
        $("#technician").hide();
        $("#service").show();
    }
}

var serviceSelectList = [];

function serviceSelectChange() {
    var input_select = $("#serviceResult").val();
    var option = $("#serviceList option");
    var option_length = option.length;
    for (var i = 0; i < option_length; i++) {
        var option_value = option.eq(i).attr("value");
        console.log("option_value: ", option_value);
        if (input_select == option_value) {
            // option_id = $("option").eq(i).attr('data-id');
            serviceSelectList.push(option_value);
            break;
        }
    }
}

function onConfirmClick() {
    var zheng = $("#select_array").find("input");
    var zStr = "[";
    if (zheng.length < 1) {
        layer.msg("请选择服务包");
    } else {
        layer.msg("开方中，请稍等..")
        for (var i = 0; i < zheng.length; i++) {
            var z1 = $(zheng[i]);
            if (z1.attr("isType") == "single") {
                zStr += '{"id":' + z1.attr('id') + '},';
            } else if (z1.attr("isType") == "more") {
                let temp = z1.attr("ids").split(",");
                $(temp).each(function (index, element) {
                    zStr += '{"id":' + element + '},';
                });
            }
        }
        zStr += "]";

        // var reqData0 = {
        //     age: memberData4.name,
        //     gender: memberData4.gender,
        //     name: memberData4.name,
        //     phone: memberData4.phone,
        //     sid: memberData4.sid,
        // };
        // $.ajax({
        //     url: $.cookie("tempUrl") + "inspect/addUmsMember" + "?" + formatParams(reqData0),
        //     type: "POST",
        //     success: function (result) {
        //         if (result.code === 200) {
        //
        //
        //         }
        //     },
        //     error: function () {
        //         layer.msg("请先选择服务医院")
        //     }
        // });
        var reqData0 = {
            doctorId: $.cookie("doctorId"),
            inspectList: zStr,
            memberId: memberData4.memberId,
            pharmInspectionId: yaodianId,
        };
        $.ajax({
            url: $.cookie("tempUrl") + "inspect/addKsInspectOrder" + "?" + formatParams(reqData0),
            type: "POST",
            success: function (result) {
                if (result.code === 200) {
                    parent.layer.closeAll()
                    parent.layer.msg("已成功开具医技")
                }
            },
            error: function () {
                layer.msg("请先选择服务医院")
            }
        });

    }
}